package com.beiing.tianshuai.tianshuai.message.ui;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.CreateGroupSearchFriendResultAdapter;
import com.beiing.tianshuai.tianshuai.entity.ContactsBean;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFriendFragment extends Fragment {

    @BindView(R.id.et_search_bar)
    EditText mEtSearchBar;
    @BindView(R.id.rv_search_result_list)
    RecyclerView mRvSearchResultList;

    private Context                                         mContext;
    private Unbinder                                        mUnbinder;
    private List<ContactsBean>                              mContacts;
    private CreateGroupSearchFriendResultAdapter            mAdapter;
    private SearchResultCheckListener                       mSearchResultCheckListener;

    public static SearchFriendFragment newInstance(List<ContactsBean> contactsList) {
        SearchFriendFragment fragment = new SearchFriendFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("contacts", (Serializable) contactsList);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void setSearchResultCheckListener(SearchResultCheckListener searchResultCheckListener) {
        mSearchResultCheckListener = searchResultCheckListener;
    }

    public interface SearchResultCheckListener{
        void onSearchResultCheckListener(ContactsBean bean, int position);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_search_friend, container, false);
        mUnbinder = ButterKnife.bind(this, contentView);
        mContext = getActivity();
        Bundle arguments = getArguments();
        if (arguments != null) {
            mContacts = (List<ContactsBean>) arguments.getSerializable("contacts");
        }
        initRecyclerView();
        initListener();
        return contentView;
    }

    private void initRecyclerView() {
        mAdapter = new CreateGroupSearchFriendResultAdapter(mContext);
        mRvSearchResultList.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRvSearchResultList.setNestedScrollingEnabled(false);
        mRvSearchResultList.setLayoutManager(linearLayoutManager);
        mRvSearchResultList.addItemDecoration(
                new RecycleViewDivider(
                        mContext, LinearLayoutManager.HORIZONTAL, 1,
                        ContextCompat.getColor(mContext, R.color.common_gray_background))
        );
    }

    private void initListener() {
        // 条目点击
        mAdapter.setListener(new CreateGroupSearchFriendResultAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ContactsBean bean) {
                bean.setChecked(!bean.isChecked());
                int i = mContacts.indexOf(bean);
                mSearchResultCheckListener.onSearchResultCheckListener(bean, i);
                LogUtils.i(String.valueOf(i), "position");
            }
        });

        // 动态搜索
        mEtSearchBar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    if (mContacts != null) {
                        List<ContactsBean> searchResultList = new ArrayList<>();
                        for (ContactsBean contact : mContacts) {
                            if (contact.getName().contains(editable.toString())) {
                                searchResultList.add(contact);
                            }
                        }
                        mAdapter.updateData(searchResultList);
                    }
                } else {
                    mAdapter.clearData();
                }
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            mAdapter.clearData();
            mEtSearchBar.setText("");
            mEtSearchBar.clearFocus();
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEtSearchBar.getWindowToken(), 0); //强制隐藏键盘
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
}
